import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_base/common/commonColors.dart';
import 'package:flutter_base/common/theme.dart';
import 'package:flutter_base/pageModel/pageModel.dart';

/// Create： Zhang 3/18/21 11:32 AM
/// 描述：页面的基础类

class PageStateful<PM extends PageModel> extends StatefulWidget {
  PageStateful({
    this.title = "标题",
    this.titleColor = Colors.black87,
    this.backgroundColor = defaultBgColor,
    required this.pageModel,
    Key? key,
  }) : super(key: key);

  final PM pageModel; //页面的数据处理类
  final String title; //标题
  final Color titleColor; //标题文字颜色
  final Color backgroundColor; //页面背景色

  ///覆写此方法则覆盖所有主题,与其方法互斥
  @protected
  Widget? rootView(BuildContext context) => null;

  ///使用默认主题,带有appBar,设置页面内容
  @protected
  Widget? content(BuildContext context) => null;

  ///覆写此方法,覆盖默认的titleBar返回按钮
  @protected
  Widget? leading(BuildContext context) => null;

  ///覆写此方法,添加底部导航栏
  @protected
  Widget? navigation(BuildContext context) => null;

  ///覆写此方法添加tabBar
  @protected
  PreferredSizeWidget? tabBar(BuildContext context) => null;

  ///覆写此方法,添加titleBar右侧action按钮
  @protected
  List<Widget>? actions(BuildContext context) => [];

  ///返回上一页
  void goBack<T extends Object?>({required BuildContext context, T? result}) {
    //不是首页返回上一页
    Navigator.pop(context, result);
  }

  ///关闭应用
  void close(BuildContext context) {
    if (Theme.of(context).platform == TargetPlatform.android) {
      //退出flutter应用,相当于小程序的关闭按钮
      SystemNavigator.pop();
    } else {
      exit(0);
    }
  }

  ///页面销毁
  void dispose() {}

  ///页面初始化
  void initState(_PageState state) {}

  ///刷新界面
  void setState() {
    pageModel.notify();
  }

  ///创建状态
  _PageState createState() => _PageState();
}

class _PageState extends State<PageStateful>
    with SingleTickerProviderStateMixin {
  @override
  void initState() {
    super.initState();
    widget.pageModel.init();
    widget.initState(this);
    //订阅页面刷新事件
    widget.pageModel.update(() => setState(() {}));
  }

  @override
  void dispose() {
    super.dispose();
    widget.dispose();
    //取消页面刷新事件的流
    widget.pageModel.onDispose();
  }

  @override
  Widget build(BuildContext context) {
    setTranStatusBar(context);
    return createBody(context);
  }

  ///创建body,优先使用rootView
  Widget createBody(BuildContext context) {
    return widget.rootView(context) ?? defaultWidget();
  }

  ///默认界面控件
  Widget defaultWidget() => Scaffold(
        appBar: AppBar(
          backgroundColor: widget.backgroundColor,
          centerTitle: true,
          //设置状态栏字体为黑色
          brightness: Brightness.light,
          elevation: 0,
          title: Text(
            widget.title,
            style: TextStyle(
              color: Colors.black,
              fontSize: 17,
              fontWeight: FontWeight.bold,
            ),
          ),
          leading: widget.leading(context) ??
              IconButton(
                color: widget.titleColor,
                icon: Icon(
                  Icons.arrow_back_ios_sharp,
                  size: 20,
                ),
                onPressed: () {
                  widget.goBack(context: context);
                },
              ),
          actions: widget.actions(context),
          bottom: widget.tabBar(context),
        ),
        body: widget.content(context),
        bottomNavigationBar: widget.navigation(context),
        backgroundColor: widget.backgroundColor,
      );
}
